<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>mongodb笔记(四) 查询 | 净土</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="mongodb querying, mongodb 查询">
<meta property="og:type" content="article">
<meta property="og:title" content="mongodb笔记(四) 查询">
<meta property="og:url" content="http://howiefh.github.io/2014/05/08/mongodb-note-4-querying/index.html">
<meta property="og:site_name" content="净土">
<meta property="og:description" content="mongodb querying, mongodb 查询">
<meta property="og:updated_time" content="2014-12-24T07:55:07.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="mongodb笔记(四) 查询">
<meta name="twitter:description" content="mongodb querying, mongodb 查询">
  
    <link rel="alternative" href="/atom.xml" title="净土" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.ico">
  
  <link href="http://fonts.useso.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="/css/style.css" type="text/css">
  
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-40492061-1', 'auto');
ga('send', 'pageview');

</script>


  
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?56d2899c5e919fbf4a7b00de5d1c31dd";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>


</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">净土</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">乐不在外而在心，心以为乐，则是境皆乐；心以为苦，则无境不苦。</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-github-link" class="nav-icon" href="https://github.com/howiefh" title="Github" target="_blank"></a>
        
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed" target="_blank"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://howiefh.github.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-mongodb-note-4-querying" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    
<a href="/2014/05/08/mongodb-note-4-querying/" class="article-date">
  <time datetime="2014-05-08T13:48:34.000Z" itemprop="datePublished">2014-05-08</time>
</a>


    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Database/">Database</a>►<a class="article-category-link" href="/categories/Database/MongoDB/">MongoDB</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      mongodb笔记(四) 查询
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
		
		<div id="toc" class="toc-article">
			<h2 class="toc-title"><span>Contents</span></h2>
		
			<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#find"><span class="toc-number">1.</span> <span class="toc-text">find</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#指定返回的键"><span class="toc-number">1.1.</span> <span class="toc-text">指定返回的键</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#限制"><span class="toc-number">1.2.</span> <span class="toc-text">限制</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#查询条件"><span class="toc-number">2.</span> <span class="toc-text">查询条件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#查询条件-1"><span class="toc-number">2.1.</span> <span class="toc-text">查询条件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#OR查询"><span class="toc-number">2.2.</span> <span class="toc-text">OR查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#$not"><span class="toc-number">2.3.</span> <span class="toc-text">$not</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#条件句的规则"><span class="toc-number">2.4.</span> <span class="toc-text">条件句的规则</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#特定于类型的查询"><span class="toc-number">3.</span> <span class="toc-text">特定于类型的查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#null"><span class="toc-number">3.1.</span> <span class="toc-text">null</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#正则表达式"><span class="toc-number">3.2.</span> <span class="toc-text">正则表达式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#查询数组"><span class="toc-number">3.3.</span> <span class="toc-text">查询数组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#范围查询"><span class="toc-number">3.4.</span> <span class="toc-text">范围查询</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#查询内嵌文档"><span class="toc-number">4.</span> <span class="toc-text">查询内嵌文档</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#$where查询"><span class="toc-number">5.</span> <span class="toc-text">$where查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#游标"><span class="toc-number">6.</span> <span class="toc-text">游标</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#limit,skip和sort"><span class="toc-number">6.1.</span> <span class="toc-text">limit,skip和sort</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#避免使用skip略过大量结果"><span class="toc-number">6.2.</span> <span class="toc-text">避免使用skip略过大量结果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#高级查询选项"><span class="toc-number">6.3.</span> <span class="toc-text">高级查询选项</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#获取一致结果"><span class="toc-number">6.4.</span> <span class="toc-text">获取一致结果</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#数据库命令"><span class="toc-number">7.</span> <span class="toc-text">数据库命令</span></a></li></ol>
		
		</div>
		
        <h2 id="find">find</h2><h3 id="指定返回的键">指定返回的键</h3><p>可以在find中通过多个键值对进行查询会被解释成and，多个键值对之间是且的关系</p>
<p>通过find的第二个参数可以指定返回的键<br><figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db.users.<span class="keyword">find</span>(&#123;&#125;,&#123;<span class="string">"username"</span>:<span class="number">1</span>,<span class="string">"email"</span>:<span class="number">1</span>&#125;)</span><br><span class="line">db.users.<span class="keyword">find</span>(&#123;&#125;,&#123;<span class="string">"username"</span>:<span class="number">1</span>,<span class="string">"email"</span>:<span class="number">1</span>,<span class="string">"_id"</span>:<span class="number">0</span>&#125;)</span><br></pre></td></tr></table></figure></p>
<p>第一条语句将返回_id,username,email,_id总是被返回。如果不想返回_id指定为0</p>
<a id="more"></a>
<h3 id="限制">限制</h3><p>查询文档的值必须是常量。也就是说不能引用文档其它键的值。<br><figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.stock.<span class="keyword">find</span>(&#123;<span class="string">"in_stock"</span>:<span class="keyword">this</span>.num_sold&#125;) <span class="comment">// 这是不可行的</span></span><br></pre></td></tr></table></figure></p>
<h2 id="查询条件">查询条件</h2><h3 id="查询条件-1">查询条件</h3><p>“$ne”,”$lt”,”$lte”,”$gt”和”$gte”分别对应”!=”,”&lt;”,”&lt;=”,”&gt;”和”&gt;=”。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.users.find(&#123;<span class="string">"age"</span>:&#123;<span class="string">"<span class="variable">$gte</span>"</span>:<span class="number">18</span>,<span class="string">"<span class="variable">$lte</span>"</span>:<span class="number">30</span>&#125;&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="OR查询">OR查询</h3><p>有两种OR查询，”$in”可以用来查询一个键的多个值。”$or”用来完成多个键值的任意给定值。</p>
<p>“$in”可以指定不同的类型的条件和值。和”$in”相对的就是”$nin”<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db.raffle.find(&#123;<span class="string">"ticket_no"</span>:&#123;<span class="string">"<span class="variable">$in</span>"</span>:[<span class="number">725</span>,<span class="number">542</span>,<span class="number">390</span>]&#125;&#125;)</span><br><span class="line">db.users.find(&#123;<span class="string">"user_id"</span>:&#123;<span class="string">"<span class="variable">$in</span>"</span>:[<span class="number">12345</span>,<span class="string">"joe"</span>]&#125;&#125;)</span><br></pre></td></tr></table></figure></p>
<p>如果数组中仅含有一个值，那么就相当于是”=”</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.users.find(&#123;<span class="string">"user_id"</span>:&#123;<span class="string">"<span class="variable">$in</span>"</span>:[<span class="number">12345</span>]&#125;&#125;)</span><br></pre></td></tr></table></figure>
<p>“$or” 接受一个包含所有可能条件的数组作为参数。也可以包含其它条件句。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.raffle.find(&#123;<span class="string">"<span class="variable">$or</span>"</span>:[&#123;<span class="string">"ticket_no"</span>:&#123;<span class="string">"<span class="variable">$in</span>"</span>:[<span class="number">725</span>,<span class="number">542</span>,<span class="number">390</span>]&#125;]&#125;,&#123;<span class="string">"winner"</span>:<span class="literal">true</span>&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="$not">$not</h3><p>“$mod”会将查询的值除以第一个给定的值，若玉树等于第二个给定的值则返回该结果。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db.users.find(&#123;<span class="string">"id_num"</span>:&#123;<span class="string">"<span class="variable">$mod</span>"</span>:[<span class="number">5</span>,<span class="number">1</span>]&#125;&#125;)</span><br><span class="line">db.users.find(&#123;<span class="string">"id_num"</span>:&#123;<span class="string">"<span class="variable">$not</span>"</span>:&#123;<span class="string">"<span class="variable">$mod</span>"</span>:[<span class="number">5</span>,<span class="number">1</span>]&#125;&#125;&#125;)</span><br></pre></td></tr></table></figure></p>
<p>第一个将返回”id_num”值为1,6,11,16等的用户，第二个将会返回”id_num”值为2,3,4,5,7,8,9,10,12等的用户。</p>
<h3 id="条件句的规则">条件句的规则</h3><p>查询条件句”$lt”等是在内层文档，而修改器是在外层文档。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db.users.find(&#123;<span class="string">"age"</span>:&#123;<span class="string">"<span class="variable">$lt</span>"</span>:<span class="number">30</span>,<span class="string">"<span class="variable">$gt</span>"</span>:<span class="number">20</span>&#125;&#125;)</span><br><span class="line">db.users.update(&#123;<span class="string">"name"</span> : <span class="string">"joe"</span>&#125;, &#123;<span class="string">"<span class="variable">$unset</span>"</span> : &#123;<span class="string">"favorite book"</span> : <span class="number">1</span>&#125;&#125;)</span><br></pre></td></tr></table></figure></p>
<p>同一个键不能对应多个更新修改器，但是查询条件句没有这个限制。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;<span class="string">"<span class="variable">$inc</span>"</span>:&#123;<span class="string">"age"</span>:<span class="number">1</span>&#125;,<span class="string">"<span class="variable">$set</span>"</span>:&#123;age:<span class="number">40</span>&#125;&#125;  //这是不可行的</span><br></pre></td></tr></table></figure></p>
<h2 id="特定于类型的查询">特定于类型的查询</h2><h3 id="null">null</h3><p>如果查询键值为null的，它不仅仅可以匹配到自身，而且匹配不存在的。<br><figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.c.<span class="keyword">find</span>(&#123;<span class="string">"z"</span>:<span class="keyword">null</span>&#125;)</span><br></pre></td></tr></table></figure></p>
<p>则”z”键为null或者”z”键不存在的都会被返回。<br>如果仅仅想匹配键值为null的文档，可以配合”$exists”来查询。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.c.find(&#123;<span class="string">"z"</span>:&#123;<span class="string">"<span class="variable">$in</span>"</span>:[null],<span class="string">"<span class="variable">$exists</span>"</span>:<span class="literal">true</span>&#125;&#125;)</span><br></pre></td></tr></table></figure></p>
<h3 id="正则表达式">正则表达式</h3><p>mongodb使用perl兼容的正则表达式(PCRE)来匹配正则表达式。</p>
<ul>
<li>mongodb 可以为前缀型正则表达式(如/^joey/)查询创建索引，所以这种类型的查询会非常高效。</li>
<li>正则表达式也可以匹配自身。<figure class="highlight elixir"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db.foo.insert(&#123;<span class="string">"bar"</span><span class="symbol">:/baz/</span>&#125;)</span><br><span class="line">db.foo.find(&#123;<span class="string">"bar"</span><span class="symbol">:/baz/</span>&#125;)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="查询数组">查询数组</h3><p>数组大多数情况下可以这样理解，每个元素都是整个键的值。<br><figure class="highlight openscad"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db.food.insert<span class="params">(&#123;<span class="string">"fruit"</span>:[<span class="string">"apple"</span>,<span class="string">"banana"</span>,<span class="string">"peach"</span>]&#125;)</span></span><br><span class="line">db.food.find<span class="params">(&#123;<span class="string">"fruit"</span>:<span class="string">"banana"</span>&#125;)</span></span><br></pre></td></tr></table></figure></p>
<p>第二条语句能够成功匹配插入的文档。</p>
<ul>
<li><p>$all</p>
<p>  通过多个元素匹配数组，就需要<code>$all</code>。</p>
<figure class="highlight openscad"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">db.food.insert<span class="params">(&#123;<span class="string">"fruit"</span>:[<span class="string">"apple"</span>,<span class="string">"banana"</span>,<span class="string">"peach"</span>]&#125;)</span></span><br><span class="line">db.food.insert<span class="params">(&#123;<span class="string">"fruit"</span>:[<span class="string">"apple"</span>,<span class="string">"kumquat"</span>,<span class="string">"orange"</span>]&#125;)</span></span><br><span class="line">db.food.insert<span class="params">(&#123;<span class="string">"fruit"</span>:[<span class="string">"cherry"</span>,<span class="string">"banana"</span>,<span class="string">"apple"</span>]&#125;)</span></span><br></pre></td></tr></table></figure>
<p>  要查找既有”apple”又有”banana”的文档，就用”$all” 来查询。</p>
<figure class="highlight less"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">db</span><span class="class">.food</span><span class="class">.find</span>(&#123;<span class="tag">fruit</span>:&#123;$<span class="attribute">all</span>:[<span class="string">"apple"</span>,<span class="string">"banana"</span>]&#125;&#125;)</span><br></pre></td></tr></table></figure>
<p>  这种方式并不关心顺序，所以apple在banana前或者后都可以匹配到。</p>
<p>  可以使用下面语句精确匹配文档。将返回第一条文档。</p>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.food.<span class="keyword">find</span>(&#123;<span class="string">"fruit"</span>:[<span class="string">"apple"</span>,<span class="string">"banana"</span>,<span class="string">"peach"</span>]&#125;)</span><br></pre></td></tr></table></figure>
<p>  但是下面两条语句不会返回任何文档。</p>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db.food.<span class="keyword">find</span>(&#123;<span class="string">"fruit"</span> : [<span class="string">"banana"</span>, <span class="string">"apple"</span>]&#125;)</span><br><span class="line">db.food.<span class="keyword">find</span>(&#123;<span class="string">"fruit"</span> : [<span class="string">"banana"</span>, <span class="string">"apple"</span>, <span class="string">"peach"</span>]&#125;)</span><br></pre></td></tr></table></figure>
<p>  如果想查找数组指定位置的元素，则需使用key.index语法指定下标。</p>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.food.<span class="keyword">find</span>(&#123;<span class="string">"fruit.2"</span>:<span class="string">"peach"</span>&#125;) <span class="comment">//下标从0开始</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>$size</p>
<p>  查询指定长度的数组。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.food.find(&#123;<span class="string">"fruit"</span>:&#123;<span class="string">"<span class="variable">$size</span>"</span>:<span class="number">3</span>&#125;&#125;)</span><br></pre></td></tr></table></figure>
<p>  “$size”不能与其他查询子句组合(如”$gt”)。可以通过在文档中添加”size”键来实现。这样在添加新元素的时候需要同时增加”size”的值。</p>
<figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">db</span>.food.<span class="keyword">update</span>(criteria,&#123;<span class="string">"$push"</span>:&#123;<span class="string">"fruit"</span>:<span class="string">"strawberry"</span>&#125;&#125;) <span class="comment">//这种更新就需要变成下面这样。</span></span><br><span class="line"><span class="keyword">db</span>.food.<span class="keyword">update</span>(criteria,&#123;<span class="string">"$push"</span>:&#123;<span class="string">"fruit"</span>:<span class="string">"strawberry"</span>&#125;,<span class="string">"$inc"</span>:&#123;<span class="string">"size"</span>:1&#125;&#125;)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.food.find(&#123;<span class="string">"size"</span>:&#123;<span class="string">"<span class="variable">$gt</span>"</span>:<span class="number">3</span>&#125;&#125;)</span><br></pre></td></tr></table></figure>
<pre><code>但是这种技巧不能和<span class="string">"<span class="variable">$addToSet</span>"</span>操作符一起使用。
</code></pre><ul>
<li><p>$slice</p>
<p>  返回数组的一个子集合</p>
<figure class="highlight openscad"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 返回前十个评论</span></span><br><span class="line">db.blog.posts.findOne<span class="params">(criteria, &#123;<span class="string">"comments"</span> : &#123;<span class="string">"$slice"</span> : <span class="number">10</span>&#125;&#125;)</span></span><br><span class="line"><span class="comment">// 返回后十个评论</span></span><br><span class="line">db.blog.posts.findOne<span class="params">(criteria, &#123;<span class="string">"comments"</span> : &#123;<span class="string">"$slice"</span> : -<span class="number">10</span>&#125;&#125;)</span></span><br><span class="line"><span class="comment">// 跳过前面23个评论，返回第24个到第33个评论</span></span><br><span class="line">db.blog.posts.findOne<span class="params">(criteria, &#123;<span class="string">"comments"</span> : &#123;<span class="string">"$slice"</span> : [<span class="number">23</span>, <span class="number">10</span>]&#125;&#125;)</span></span><br></pre></td></tr></table></figure>
<p>  别的键的说明符都是默认不返回未提及的键，”$slice”返回文档中的所有键。</p>
</li>
</ul>
<h3 id="范围查询">范围查询</h3><p>假设有下面的文档。要查找x大于10小于20的文档。</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#123;"<span class="attribute">x</span>" : <span class="value"><span class="number">5</span></span>&#125;</span><br><span class="line">&#123;"<span class="attribute">x</span>" : <span class="value"><span class="number">15</span></span>&#125;</span><br><span class="line">&#123;"<span class="attribute">x</span>" : <span class="value"><span class="number">25</span></span>&#125;</span><br><span class="line">&#123;"<span class="attribute">x</span>" : <span class="value">[<span class="number">5</span>, <span class="number">25</span>]</span>&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.test.find(&#123;<span class="string">"x"</span> : &#123;<span class="string">"<span class="variable">$gt</span>"</span> : <span class="number">10</span>, <span class="string">"<span class="variable">$lt</span>"</span> : <span class="number">20</span>&#125;&#125;)</span><br></pre></td></tr></table></figure>
<p>这样将会返回<code>{&quot;x&quot; : 15} {&quot;x&quot; : [5, 25]}</code> 因为5满足小于20，25满足大于10。</p>
<p>如果使用<code>$elemMatch</code>则只会匹配数组。非数组元素不会匹配。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.test.find(&#123;<span class="string">"x"</span> : &#123;<span class="string">"<span class="variable">$elemMatch</span>"</span> : &#123;<span class="string">"<span class="variable">$gt</span>"</span> : <span class="number">10</span>, <span class="string">"<span class="variable">$lt</span>"</span> : <span class="number">20</span>&#125;&#125;)</span><br></pre></td></tr></table></figure></p>
<p>使用下面的查询，则会返回{“x”:15}<br><figure class="highlight openscad"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.test.find<span class="params">(&#123;<span class="string">"x"</span> : &#123;<span class="string">"$gt"</span> : <span class="number">10</span>, <span class="string">"$lt"</span> : <span class="number">20</span>&#125;)</span>.<span class="built_in">min</span><span class="params">(&#123;<span class="string">"x"</span> : <span class="number">10</span>&#125;)</span>.<span class="built_in">max</span><span class="params">(&#123;<span class="string">"x"</span> : <span class="number">20</span>&#125;)</span></span><br></pre></td></tr></table></figure></p>
<h2 id="查询内嵌文档">查询内嵌文档</h2><p>假设有文档如下<br><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">"<span class="attribute">name</span>" : <span class="value">&#123;</span><br><span class="line">"<span class="attribute">first</span>" : <span class="value"><span class="string">"Joe"</span></span>,</span><br><span class="line">"<span class="attribute">last</span>" : <span class="value"><span class="string">"Schmoe"</span></span><br><span class="line"></span>&#125;</span>,</span><br><span class="line">"<span class="attribute">age</span>" : <span class="value"><span class="number">45</span></span><br><span class="line"></span>&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">db.people.<span class="keyword">find</span>(&#123;<span class="string">"name"</span> : &#123;<span class="string">"first"</span> : <span class="string">"Joe"</span>, <span class="string">"last"</span> : <span class="string">"Schmoe"</span>&#125;&#125;)</span><br><span class="line">db.people.<span class="keyword">find</span>(&#123;<span class="string">"name"</span> : &#123;<span class="string">"last"</span> : <span class="string">"Schmoe"</span>, <span class="string">"first"</span> : <span class="string">"Joe"</span>&#125;&#125;)</span><br><span class="line">db.people.<span class="keyword">find</span>(&#123;<span class="string">"name.first"</span> : <span class="string">"Joe"</span>, <span class="string">"name.last"</span> : <span class="string">"Schmoe"</span>&#125;)</span><br></pre></td></tr></table></figure>
<p>上面的几条语句，第一条会成功返回这个文档，第二条则什么也不会返回，这是因为这种查询是与顺序相关的(数组，内嵌文档都与顺序有关)。第三条查询相对是比较好的，这样即使Joe又增加了其它键，依然可以匹配到。</p>
<p>现在有下面的文档，我们要查找joe评分大于等于5的文档。<br><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">"<span class="attribute">content</span>" : <span class="value"><span class="string">"..."</span></span>,</span><br><span class="line">"<span class="attribute">comments</span>" : <span class="value">[</span><br><span class="line">&#123;</span><br><span class="line">"<span class="attribute">author</span>" : <span class="value"><span class="string">"joe"</span></span>,</span><br><span class="line">"<span class="attribute">score</span>" : <span class="value"><span class="number">3</span></span>,</span><br><span class="line">"<span class="attribute">comment</span>" : <span class="value"><span class="string">"nice post"</span></span><br><span class="line"></span>&#125;,</span><br><span class="line">&#123;</span><br><span class="line">"<span class="attribute">author</span>" : <span class="value"><span class="string">"mary"</span></span>,</span><br><span class="line">"<span class="attribute">score</span>" : <span class="value"><span class="number">6</span></span>,</span><br><span class="line">"<span class="attribute">comment</span>" : <span class="value"><span class="string">"terrible post"</span></span><br><span class="line"></span>&#125;</span><br><span class="line">]</span><br><span class="line"></span>&#125;</span><br></pre></td></tr></table></figure></p>
<p>不能使用下面的语句查询，内嵌文档要求匹配整个文档，这里没有<code>comment</code>键。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.blog.find(&#123;<span class="string">"comments.author"</span> : <span class="string">"joe"</span>, <span class="string">"comments.score"</span> : &#123;<span class="string">"<span class="variable">$gte</span>"</span> : <span class="number">5</span>&#125;&#125;)</span><br></pre></td></tr></table></figure></p>
<p>下面语句会返回之前的文档，第一条评论满足author为joe，第二条评论满足score大于等于5。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.blog.find(&#123;<span class="string">"comments.author"</span> : <span class="string">"joe"</span>, <span class="string">"comments.score"</span> : &#123;<span class="string">"<span class="variable">$gte</span>"</span> : <span class="number">5</span>&#125;&#125;)</span><br></pre></td></tr></table></figure></p>
<p>下面的语句能够正确返回查询结果，要指定一组查询条件，而又不指定每个键，需要使用<code>$elemMatch</code><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.blog.find(&#123;<span class="string">"comments"</span> : &#123;<span class="string">"<span class="variable">$elemMatch</span>"</span> : &#123;<span class="string">"author"</span> : <span class="string">"joe"</span>, <span class="string">"score"</span> : &#123;<span class="string">"<span class="variable">$gte</span>"</span> : <span class="number">5</span>&#125;&#125;&#125;&#125;)</span><br></pre></td></tr></table></figure></p>
<h2 id="$where查询">$where查询</h2><figure class="highlight openscad"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db.foo.insert<span class="params">(&#123;<span class="string">"apple"</span> : <span class="number">1</span>, <span class="string">"banana"</span> : <span class="number">6</span>, <span class="string">"peach"</span> : <span class="number">3</span>&#125;)</span></span><br><span class="line">db.foo.insert<span class="params">(&#123;<span class="string">"apple"</span> : <span class="number">8</span>, <span class="string">"spinach"</span> : <span class="number">4</span>, <span class="string">"watermelon"</span> : <span class="number">4</span>&#125;)</span></span><br></pre></td></tr></table></figure>
<p>通过$where语句可以查询两种水果数量相同的文档。<br><figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">db.foo.find(&#123;<span class="string">"$where"</span> : <span class="function"><span class="keyword">function</span> <span class="params">()</span> </span>&#123;</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> current <span class="keyword">in</span> <span class="keyword">this</span>) &#123;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">var</span> other <span class="keyword">in</span> <span class="keyword">this</span>) &#123;</span><br><span class="line"> <span class="keyword">if</span> (current != other &amp;&amp; <span class="keyword">this</span>[current] == <span class="keyword">this</span>[other]) &#123;</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;&#125;);</span><br></pre></td></tr></table></figure></p>
<p>无非必要，不要使用$where，因为它会把文档从BSON转换成javascript对象。然后通过$where表达式运行。所以速度会比较慢。可以使用常规的查询先过滤一部分结果，再由$where处理。</p>
<h2 id="游标">游标</h2><figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="built_in">i</span>=<span class="number">0</span>; <span class="built_in">i</span>&lt;<span class="number">100</span>; <span class="built_in">i</span>++) <span class="cell">&#123;</span><br><span class="line"> db.collection.insert(&#123;x : i&#125;</span>);</span><br><span class="line">&#125;</span><br><span class="line">var cursor = db.collection.<span class="built_in">find</span>();</span><br></pre></td></tr></table></figure>
<p>当调用find的时候，shell并不立即查询数据库，而是等待真正开始要求获得结果的时候才发送查询，这样在执行之前可以给查询附加额外选项。几乎所有游标对象的方法都返回游标本身，这样可以按任意顺序组成方法链。<br><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable"><span class="keyword">var</span> cursor</span> = db.collection.find();</span><br></pre></td></tr></table></figure></p>
<p>这时，查询并没有执行，当执行<code>cursor.hasNext()</code>的时候，查询被发送到服务器，shell立即获取前100个结果或者4mb数据(取最小)</p>
<h3 id="limit,skip和sort">limit,skip和sort</h3><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db<span class="class">.c</span><span class="class">.find</span>().<span class="function"><span class="title">limit</span><span class="params">(<span class="number">3</span>)</span></span></span><br></pre></td></tr></table></figure>
<p>截取查询结果的前三个<br><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db<span class="class">.c</span><span class="class">.find</span>().<span class="function"><span class="title">skip</span><span class="params">(<span class="number">3</span>)</span></span></span><br></pre></td></tr></table></figure></p>
<p>略过查询结果的前三个<br><figure class="highlight less"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">db</span><span class="class">.c</span><span class="class">.find</span>()<span class="class">.sort</span>(&#123;<span class="attribute">username</span>:<span class="number">1</span>,<span class="attribute">age</span>:-<span class="number">1</span>&#125;)</span><br></pre></td></tr></table></figure></p>
<p>排序方向1是升序，-1是降序<br><figure class="highlight openscad"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db.stock.find<span class="params">(&#123;<span class="string">"desc"</span> : <span class="string">"mp3"</span>&#125;)</span>.limit<span class="params">(<span class="number">50</span>)</span>.sort<span class="params">(&#123;<span class="string">"price"</span> : -<span class="number">1</span>&#125;)</span></span><br><span class="line">db.stock.find<span class="params">(&#123;<span class="string">"desc"</span> : <span class="string">"mp3"</span>&#125;)</span>.limit<span class="params">(<span class="number">50</span>)</span>.skip<span class="params">(<span class="number">50</span>)</span>.sort<span class="params">(&#123;<span class="string">"price"</span> : -<span class="number">1</span>&#125;)</span></span><br></pre></td></tr></table></figure></p>
<p>上面两句可以实现下一页功能。略过太多项的话会带来性能问题。<br>一个键的值可能是不同的类型。不同类型数据比较顺序:</p>
<ol>
<li>最小值</li>
<li>null</li>
<li>数字</li>
<li>字符串</li>
<li>对象/文档</li>
<li>数组</li>
<li>二进制数据</li>
<li>对象ID</li>
<li>布尔型</li>
<li>日期型</li>
<li>时间戳</li>
<li>正则表达式</li>
<li>最大值</li>
</ol>
<h3 id="避免使用skip略过大量结果">避免使用skip略过大量结果</h3><ul>
<li><p>不用skip对结果分页</p>
<p> 要尽量避免使用skip对结果分页。 </p>
<figure class="highlight openscad"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">var page1 = db.foo.find<span class="params">()</span>.sort<span class="params">(&#123;<span class="string">"date"</span> : -<span class="number">1</span>&#125;)</span>.limit<span class="params">(<span class="number">100</span>)</span></span><br><span class="line">var latest = null;</span><br><span class="line"><span class="comment">// display first page</span></span><br><span class="line">while <span class="params">(page1.hasNext<span class="params">()</span>)</span> &#123;</span><br><span class="line">latest = page1.next<span class="params">()</span>;</span><br><span class="line">display<span class="params">(latest)</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// get next page</span></span><br><span class="line">var page2 = db.foo.find<span class="params">(&#123;<span class="string">"date"</span> : &#123;<span class="string">"$gt"</span> : latest.date&#125;&#125;)</span>;</span><br><span class="line">page2.sort<span class="params">(&#123;<span class="string">"date"</span> : -<span class="number">1</span>&#125;)</span>.limit<span class="params">(<span class="number">100</span>)</span>;</span><br></pre></td></tr></table></figure>
<p>  像上面这种查询中就没有skip了。</p>
</li>
<li><p>随机选取文档</p>
<p>  如果先计算文档总数，然后从0到文档总数之间取一个随机数，再略过这个随机数那么多文档，这种方式效率是很低的。</p>
<p>  可以在文档中多加一个随机数的键。</p>
<figure class="highlight openscad"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">db.people.insert<span class="params">(&#123;<span class="string">"name"</span> : <span class="string">"joe"</span>, <span class="string">"random"</span> : Math.random<span class="params">()</span>&#125;)</span></span><br><span class="line">db.people.insert<span class="params">(&#123;<span class="string">"name"</span> : <span class="string">"john"</span>, <span class="string">"random"</span> : Math.random<span class="params">()</span>&#125;)</span></span><br><span class="line">db.people.insert<span class="params">(&#123;<span class="string">"name"</span> : <span class="string">"jim"</span>, <span class="string">"random"</span> : Math.random<span class="params">()</span>&#125;)</span></span><br></pre></td></tr></table></figure>
<p>  取随机文档只需要再生成一个随机数，然后匹配就好了。</p>
<figure class="highlight nimrod"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> random = <span class="type">Math</span>.random()</span><br><span class="line"><span class="literal">result</span> = db.foo.findOne(&#123;<span class="string">"random"</span> : &#123;<span class="string">"$gt"</span> : random&#125;&#125;)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="高级查询选项">高级查询选项</h3><p>查询分为普通的和包装的<br><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> cursor = <span class="keyword">db</span>.foo.find(&#123;<span class="string">"foo"</span> : <span class="string">"bar"</span>&#125;)</span><br><span class="line"><span class="keyword">sort</span>:</span><br><span class="line"><span class="keyword">var</span> cursor = <span class="keyword">db</span>.foo.find(&#123;<span class="string">"foo"</span> : <span class="string">"bar"</span>&#125;).<span class="keyword">sort</span>(&#123;<span class="string">"x"</span> : 1&#125;)</span><br></pre></td></tr></table></figure></p>
<p>第一条就是普通的，第二条是包装的。实际shell会将其转换为<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;<span class="string">"<span class="variable">$query</span>"</span> : &#123;<span class="string">"foo"</span> : <span class="string">"bar"</span>&#125;, <span class="string">"<span class="variable">$orderby</span>"</span> : &#123;<span class="string">"x"</span> : <span class="number">1</span>&#125;&#125;.</span><br></pre></td></tr></table></figure></p>
<p>可以向查询添加选项</p>
<ul>
<li><p>$maxscan : integer</p>
<p>  指定查询最多扫描的文档数量</p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db<span class="class">.foo</span><span class="class">.find</span>(criteria)._addSpecial(<span class="string">"$maxscan"</span>, <span class="number">20</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>$min : document</p>
<p>  查询的开始条件</p>
</li>
<li><p>$max : document</p>
<p>  查询的结束条件</p>
</li>
<li><p>$showDiskLoc : true</p>
<p>  显示查询结果在磁盘中的位置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt; db.foo.find()._addSpecial(<span class="string">'$showDiskLoc'</span>,<span class="literal">true</span>)</span><br><span class="line">&#123; <span class="string">"_id"</span> : <span class="number">0</span>, <span class="string">"<span class="variable">$diskLoc</span>"</span> : &#123; <span class="string">"file"</span> : <span class="number">2</span>, <span class="string">"offset"</span> : <span class="number">154812592</span> &#125; &#125;</span><br><span class="line">&#123; <span class="string">"_id"</span> : <span class="number">1</span>, <span class="string">"<span class="variable">$diskLoc</span>"</span> : &#123; <span class="string">"file"</span> : <span class="number">2</span>, <span class="string">"offset"</span> : <span class="number">154812628</span> &#125; &#125;</span><br></pre></td></tr></table></figure>
<p>  第一项是结果所在文件，建设数据是test，那么查询结果在test.2中，第二项是结果在文件中的偏移量。</p>
</li>
<li><p>$hint : document</p>
<p>  指定服务器使用哪个索引来进行查询</p>
</li>
<li><p>$explain : boolean</p>
<p>  获取查询执行的细节(用到的索引、结果数量、耗时等)，并非真正执行查询</p>
</li>
<li><p>$snapshot : boolean</p>
<p>  确保查询的结果是在查询执行的那一刻的一致快照</p>
</li>
</ul>
<h3 id="获取一致结果">获取一致结果</h3><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db<span class="class">.foo</span><span class="class">.find</span>().<span class="function"><span class="title">snapshot</span><span class="params">()</span></span></span><br></pre></td></tr></table></figure>
<pre><code>使用快照可以获取一致结果
</code></pre><h2 id="数据库命令">数据库命令</h2><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&gt; <span class="keyword">db</span>.runCommand(&#123;<span class="string">"drop"</span> : <span class="string">"test"</span>&#125;);</span><br><span class="line">&gt; <span class="keyword">use</span> temp</span><br><span class="line">switched to <span class="keyword">db</span> temp</span><br><span class="line">&gt; <span class="keyword">db</span>.runCommand(&#123;shutdown:1&#125;)</span><br><span class="line">&#123; <span class="string">"errmsg"</span> : <span class="string">"access denied; use admin db"</span>, <span class="string">"ok"</span> : 0 &#125;</span><br><span class="line">&gt; <span class="keyword">db</span>.adminCommand(&#123;<span class="string">"shutdown"</span> : 1&#125;)</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
	  
	  <!-- 百度分享 Start -->
	  <div class="bdsharebuttonbox"><a href="#" class="bds_more" data-cmd="more"></a><a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a><a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a><a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博"></a><a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网"></a><a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a></div>
	  <!-- 百度分享 End -->
	  
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/MongoDB/">MongoDB</a></li></ul>

	  
<span>
Updated:<time datetime="2014-12-24T07:55:07.000Z" itemprop="dateModified">2014-12-24</time>
</span>


    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2014/05/26/mongodb-note-5-indexing/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          mongodb笔记(五) 索引
        
      </div>
    </a>
  
  
    <a href="/2014/05/04/mongodb-note-3-creating-updating-and-deleting-documents/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">mongodb笔记(三) 创建、更新、删除文档</div>
    </a>
  
</nav>

  
</article>



<!-- 多说评论框 start -->

<section id="comments">
  <div class="ds-thread"  data-thread-key="/2014/05/08/mongodb-note-4-querying/" data-title="mongodb笔记(四) 查询" data-url="http://howiefh.github.io/2014/05/08/mongodb-note-4-querying/" id="ds_thread">
    <noscript>Please enable JavaScript to view the <a href="//duoshuo.com/?ref_noscript">comments powered by duoshuo.</a></noscript>
  </div>
</section>

<!-- 多说评论框 end -->
</section>
        
          
  <div id="toc" class="toc-aside">
  <h2 class="toc-title">Contents</h2>
    
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#find"><span class="toc-number">1.</span> <span class="toc-text">find</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#指定返回的键"><span class="toc-number">1.1.</span> <span class="toc-text">指定返回的键</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#限制"><span class="toc-number">1.2.</span> <span class="toc-text">限制</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#查询条件"><span class="toc-number">2.</span> <span class="toc-text">查询条件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#查询条件-1"><span class="toc-number">2.1.</span> <span class="toc-text">查询条件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#OR查询"><span class="toc-number">2.2.</span> <span class="toc-text">OR查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#$not"><span class="toc-number">2.3.</span> <span class="toc-text">$not</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#条件句的规则"><span class="toc-number">2.4.</span> <span class="toc-text">条件句的规则</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#特定于类型的查询"><span class="toc-number">3.</span> <span class="toc-text">特定于类型的查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#null"><span class="toc-number">3.1.</span> <span class="toc-text">null</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#正则表达式"><span class="toc-number">3.2.</span> <span class="toc-text">正则表达式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#查询数组"><span class="toc-number">3.3.</span> <span class="toc-text">查询数组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#范围查询"><span class="toc-number">3.4.</span> <span class="toc-text">范围查询</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#查询内嵌文档"><span class="toc-number">4.</span> <span class="toc-text">查询内嵌文档</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#$where查询"><span class="toc-number">5.</span> <span class="toc-text">$where查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#游标"><span class="toc-number">6.</span> <span class="toc-text">游标</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#limit,skip和sort"><span class="toc-number">6.1.</span> <span class="toc-text">limit,skip和sort</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#避免使用skip略过大量结果"><span class="toc-number">6.2.</span> <span class="toc-text">避免使用skip略过大量结果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#高级查询选项"><span class="toc-number">6.3.</span> <span class="toc-text">高级查询选项</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#获取一致结果"><span class="toc-number">6.4.</span> <span class="toc-text">获取一致结果</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#数据库命令"><span class="toc-number">7.</span> <span class="toc-text">数据库命令</span></a></li></ol>
    
  </div>

<aside id="sidebar">

  
    
<div class="widget-wrap">
  <h3 class="widget-title">ABOUT ME</h3>
  <ul class="widget about-me">
    
    <li><img class="author" title="About me" src="http://fh-1.qiniudn.com/okal-eltocat.jpg" /></li>
    
    
    <li>Hi,I'm FengHao.</li>
    
    <li>I'll share something interesting and my learning experience with you at this blog.</li>
    
    <li>前博客:<a href="http://hi.baidu.com/idea_star" target="_BLANK">百度空间</a></li>
    
  </ul>
</div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Android/">Android</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/C/">C</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Database/">Database</a><span class="category-list-count">13</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Database/MongoDB/">MongoDB</a><span class="category-list-count">10</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Database/MySQL/">MySQL</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/Eclipse/">Eclipse</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/FTP/">FTP</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Git/">Git</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Hexo/">Hexo</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java/">Java</a><span class="category-list-count">20</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Java/FreeMarker/">FreeMarker</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java/Shiro/">Shiro</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaEE/">JavaEE</a><span class="category-list-count">4</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/JavaEE/Hibernate/">Hibernate</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaEE/JSP/">JSP</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaEE/Spring/">Spring</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaScript/">JavaScript</a><span class="category-list-count">5</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/JavaScript/jQuery/">jQuery</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/Linux/">Linux</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/PHP/">PHP</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Suse/">Suse</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Ubuntu/">Ubuntu</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Vim/">Vim</a><span class="category-list-count">8</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/编程/">编程</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/软件/">软件</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Android/" style="font-size: 14.75px; color: #65bfa7">Android</a> <a href="/tags/C/" style="font-size: 13.88px; color: #71c1c2">C</a> <a href="/tags/CAS/" style="font-size: 13px; color: #7dc3de">CAS</a> <a href="/tags/Chrome/" style="font-size: 13px; color: #7dc3de">Chrome</a> <a href="/tags/Clean-Code/" style="font-size: 13.88px; color: #71c1c2">Clean Code</a> <a href="/tags/Database/" style="font-size: 13px; color: #7dc3de">Database</a> <a href="/tags/Eclipse/" style="font-size: 13px; color: #7dc3de">Eclipse</a> <a href="/tags/FTP/" style="font-size: 13.88px; color: #71c1c2">FTP</a> <a href="/tags/FreeMarker/" style="font-size: 14.75px; color: #65bfa7">FreeMarker</a> <a href="/tags/Gcc/" style="font-size: 13px; color: #7dc3de">Gcc</a> <a href="/tags/Git/" style="font-size: 14.75px; color: #65bfa7">Git</a> <a href="/tags/Github-Pages/" style="font-size: 13px; color: #7dc3de">Github Pages</a> <a href="/tags/Hexo/" style="font-size: 14.75px; color: #65bfa7">Hexo</a> <a href="/tags/Hibernate/" style="font-size: 13px; color: #7dc3de">Hibernate</a> <a href="/tags/JSP/" style="font-size: 13px; color: #7dc3de">JSP</a> <a href="/tags/JVM/" style="font-size: 14.75px; color: #65bfa7">JVM</a> <a href="/tags/Java/" style="font-size: 14.75px; color: #65bfa7">Java</a> <a href="/tags/JavaMail/" style="font-size: 13px; color: #7dc3de">JavaMail</a> <a href="/tags/JavaScript/" style="font-size: 16.5px; color: #4dbc6f">JavaScript</a> <a href="/tags/Linux/" style="font-size: 13.88px; color: #71c1c2">Linux</a> <a href="/tags/Log/" style="font-size: 13px; color: #7dc3de">Log</a> <a href="/tags/Markdown/" style="font-size: 13.88px; color: #71c1c2">Markdown</a> <a href="/tags/MongoDB/" style="font-size: 20px; color: #1db400">MongoDB</a> <a href="/tags/MySQL/" style="font-size: 13.88px; color: #71c1c2">MySQL</a> <a href="/tags/PHP/" style="font-size: 16.5px; color: #4dbc6f">PHP</a> <a href="/tags/Rhythmbox/" style="font-size: 13px; color: #7dc3de">Rhythmbox</a> <a href="/tags/SSO/" style="font-size: 13px; color: #7dc3de">SSO</a> <a href="/tags/Servlet/" style="font-size: 13px; color: #7dc3de">Servlet</a> <a href="/tags/Shiro/" style="font-size: 13.88px; color: #71c1c2">Shiro</a> <a href="/tags/Spring/" style="font-size: 13.88px; color: #71c1c2">Spring</a> <a href="/tags/Suse/" style="font-size: 13px; color: #7dc3de">Suse</a> <a href="/tags/Thinking-in-Java/" style="font-size: 19.13px; color: #29b61c">Thinking in Java</a> <a href="/tags/Ubuntu/" style="font-size: 17.38px; color: #41ba53">Ubuntu</a> <a href="/tags/Vim/" style="font-size: 18.25px; color: #35b838">Vim</a> <a href="/tags/VirtualBox/" style="font-size: 13px; color: #7dc3de">VirtualBox</a> <a href="/tags/Vsftpd/" style="font-size: 13px; color: #7dc3de">Vsftpd</a> <a href="/tags/jQuery/" style="font-size: 13px; color: #7dc3de">jQuery</a> <a href="/tags/pam-mysql/" style="font-size: 13px; color: #7dc3de">pam_mysql</a> <a href="/tags/小米/" style="font-size: 13px; color: #7dc3de">小米</a> <a href="/tags/软件/" style="font-size: 15.63px; color: #59bd8b">软件</a>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/09/">September 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/08/">August 2015</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/07/">July 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/05/">May 2015</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/04/">April 2015</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/03/">March 2015</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/02/">February 2015</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/12/">December 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/11/">November 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/10/">October 2014</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/09/">September 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/08/">August 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/07/">July 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/06/">June 2014</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/05/">May 2014</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/04/">April 2014</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/03/">March 2014</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/02/">February 2014</a><span class="archive-list-count">11</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/01/">January 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/12/">December 2013</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/11/">November 2013</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/08/">August 2013</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/07/">July 2013</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/06/">June 2013</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/05/">May 2013</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/04/">April 2013</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/03/">March 2012</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>


  
    <div class="widget-wrap">
  <h3 class="widget-title">Calendar</h3>
  <div class="widget">
    <div id="g-calendar" class="g-calendar">
        <span class="g-calendar-prev"></span>
		 
        <span class="g-calendar-back"></span>
        <span class="g-calendar-now"></span>
		 
        <span class="g-calendar-next"></span>
        <div class="g-calendar-hd"></div>
        <div class="g-calendar-tit"></div>
        <div class="g-calendar-bd"></div>
    </div>
  </div>
</div>
<script type="text/javascript">
function LGY_calendar(wrapId, options){
    this.oWrap = this.getId(wrapId);
    this.oHead = this.getByClassName('g-calendar-hd',this.oWrap)[0];
    this.oBody = this.getByClassName('g-calendar-bd',this.oWrap)[0];
    this.oTit = this.getByClassName('g-calendar-tit',this.oWrap)[0];
    this.oPrev = this.getByClassName('g-calendar-prev',this.oWrap)[0];
    this.oNext = this.getByClassName('g-calendar-next',this.oWrap)[0];
    this.oNow = this.getByClassName('g-calendar-now',this.oWrap)[0];
    this.oBack = this.getByClassName('g-calendar-back',this.oWrap)[0];
    this.init();
}
LGY_calendar.prototype = {
    ///////////获取ID元素
    getId:function(id){
        return document.getElementById(id);
    },
    ////////获取css类名元素
    getByClassName:function(className,parent){
        var elem = [],
            node = parent != undefined&&parent.nodeType==1?parent.getElementsByTagName('*'):document.getElementsByTagName('*'),
            p = new RegExp("(^|\\s)"+className+"(\\s|$)");
        for(var n=0,i=node.length;n<i;n++){
            if(p.test(node[n].className)){
                elem.push(node[n]);
            }
        }
        return elem;
    },
    //填充日历
    fillDate:function(year,month){
        //本月份第一天是星期几-为显示上个月的天数做铺垫
        var first_day = new Date(year,month,1).getDay(),
        //如果刚好是星期天，则空出一行（显示上个月的天数）
            first_day = first_day == 0?first_day=7:first_day;
        //本月份最后一天是几号
        var final_date = new Date(year,month+1,0).getDate(),
        //上个月的最后一天是几号
            last_date = new Date(year,month,0).getDate(),
        //剩余的格子数--即排在末尾的格子数
            surplus = 42 - first_day - final_date;
        //设置年的链接
        var yearHead = "<a href='/" + "archives/" + year + "/'>" + year + " "+ "</a>"; 
        //设置年的链接
        var monthHead = "";
        var month1 = month + 1;
        if (month1 < 10) {
            monthHead = "<a href='/" + "archives/" + year + "/" + "0" + month1 + "/'>" + " " + month1 + " " + "</a>";
        } else {
            monthHead = "<a href='/" + "archives/" + year + "/" + month1 + "/'>" + " " + month1 + " " + "</a>";
        }
        //设置表头的日历
        this.oHead.innerHTML = yearHead+'年'+monthHead+'月';
        //填充日历执行
        var html = '';
        //上个月的显示天数
        for(var i=0;i<first_day;i++){
            html+='<span class="g-calendar-grey">'+(last_date-(first_day-1)+i)+'</span>';
        }
        //本月的显示天数
        var postdate = new Date("Thu May 08 2014 21:48:34 GMT+0800"); 
        if (true && postdate.getFullYear() == year && postdate.getMonth() == month) { 
            html += '<span>1</span><span>2</span><span>3</span><span><a href="/2014/05/04/mongodb-note-3-creating-updating-and-deleting-documents/" title="mongodb笔记(三) 创建、更新、删除文档">4</a></span><span>5</span><span>6</span><span>7</span><span><a href="/2014/05/08/mongodb-note-4-querying/" title="mongodb笔记(四) 查询">8</a></span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span><span>25</span><span><a href="/2014/05/26/mongodb-note-5-indexing/" title="mongodb笔记(五) 索引">26</a></span><span>27</span><span><a href="/2014/05/28/mongodb-note-6-aggregation/" title="mongodb笔记(六) 聚合">28</a></span><span>29</span><span>30</span>';
        } else {
            for(var j=0;j<final_date;j++){        
                html+='<span id="d'+(j+1)+'">'+(j+1)+'</span>';
            }
        }
        //下个月的显示天数
        for(var k=0;k<surplus;k++){
            html+='<span class="g-calendar-grey">'+(k+1)+'</span>';
        }
        //fill
        this.oBody.innerHTML = html;
        // 当前状态
        if(year==this.c_year&&this.c_month==month){
            this.oBody.getElementsByTagName('span')[first_day+this.date-1].className='g-calendar-on';
        }

        // 对所有文章遍历,然后将有文章的日期加上链接,如果文章太多的话,生成页面会很大,去掉了
        
    },
    // next切换
    next:function(){
        var _that = this;
        this.oNext.onclick = function(){
            _that.month++;
            if(_that.month>11){
                _that.month = 0;
                _that.year++;
            }
            // 填充日历
            _that.fillDate(_that.year,_that.month);
        };
    },
    // back 切换
    back:function(){
        var _that = this;
        if(this.oBack != undefined) {
            this.oBack.onclick = function(){
                var postdate = new Date("Thu May 08 2014 21:48:34 GMT+0800"); 
                _that.year = postdate.getFullYear();
                _that.month = postdate.getMonth();
                // 填充日历
                _that.fillDate(_that.year,_that.month);
            };
        }
    },
    // now切换
    now:function(){
        var _that = this;
        if(this.oNow != undefined ) {
            this.oNow.onclick = function(){
                var nowDate = new Date(); 
                _that.year = nowDate.getFullYear();
                _that.month = nowDate.getMonth();
                // 填充日历
                _that.fillDate(_that.year,_that.month);
            };
        }
    },
    // prev切换
    prev:function(){
        var _that = this;
        this.oPrev.onclick = function(){
            _that.month--;
            if(_that.month<0){
                _that.month = 11;
                _that.year--;
            }
            // 填充日历
            _that.fillDate(_that.year,_that.month);
        };
    },
    init:function(){
        this.oTit.innerHTML = '<span>日</span><span>一</span><span>二</span><span>三</span><span>四</span><span>五</span><span>六</span>';
        // 获取今天的日历时间
        var now = new Date();
        this.c_year = this.year = now.getFullYear();
        this.c_month = this.month = now.getMonth();
        this.date = now.getDate();
        // 初始化--填充日历
        this.fillDate(this.year,this.month);
        //next切换
        this.next();
        //prev切换
        this.prev();
        //back 切换
        this.back();
        //now 切换
        this.now();
    }
}
new LGY_calendar('g-calendar');
</script>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2015/09/01/javascript-summary/">JavaScript 总结</a>
          </li>
        
          <li>
            <a href="/2015/08/28/javascript-oop-function-expression-and-async/">JavaScript 面向对象程序设计、函数表达式和异步编程</a>
          </li>
        
          <li>
            <a href="/2015/08/28/javascript-reference-type/">JavaScript 引用类型</a>
          </li>
        
          <li>
            <a href="/2015/08/28/javascript-grammar/">JavaScript 基本语法</a>
          </li>
        
          <li>
            <a href="/2015/08/10/java-web/">Java Web 笔记</a>
          </li>
        
      </ul>
    </div>
  </div>


  
    
<div class="widget-wrap">
  <h3 class="widget-title">Recent Comments</h3>
  <ul class="widget ds-recent-comments" data-num-items="5" data-show-avatars="0" data-show-title="1" data-show-time="1"></ul>
</div>
<!-- 需要多说的公用代码 -->


  

</aside>

        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2015 howiefh<br>
      Powered by <a href="http://zespia.tw/hexo/" target="_blank">Hexo</a> and Theme by <a href="https://github.com/howiefh/hexo-theme-landscape-f" target="_blank" title="Landscape-F">Landscape-F</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<!-- 多说公共JS代码 start (一个网页只需插入一次) -->

<script type="text/javascript">
  var duoshuoQuery = {short_name:"howiefh"};
  (function() {
	var ds = document.createElement('script');
	ds.type = 'text/javascript';ds.async = true;
	ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
	ds.charset = 'UTF-8';
	(document.getElementsByTagName('head')[0] 
		|| document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
</script> 

<!-- 多说公共JS代码 end -->

<!-- 百度分享 start -->

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":["mshare","douban","bdysc","sqq","qq","hi","baidu","huaban","youdao","sdo","mail","xg","diandian","fx","copy","print"],"bdPic":"","bdStyle":"1","bdSize":"16"},"share":{},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到：","viewSize":"16"}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>

<!-- 百度分享 end -->

<!--
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="/js/jquery.min.js"></script>
-->
<script src="/js/jquery.min.js" type="text/javascript"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
  <script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>


<div class="bottom-btn">

	<a class="icon-gotop" href="javascript:void(0)" title="返回顶部"></a>
	<script src="/js/gotop.js" type="text/javascript"></script>
	<!--
	<script src="/js/gotop.js"></script>
	-->


	<a class="icon-toc-toggle" href="javascript:void(0)" title="文章目录"></a>
	<!--
	<script src="/js/toc_aside_toggle.js"></script>
	-->

</div>
<script src="/js/toc_aside_toggle.js" type="text/javascript"></script>


<script src="/js/script.js" type="text/javascript"></script>

  </div>
</body>
</html>
